package com.bilubi.reggie.reggie.controller;

import com.bilubi.reggie.reggie.bean.ShoppingCart;
import com.bilubi.reggie.reggie.bean.User;
import com.bilubi.reggie.reggie.common.R;
import com.bilubi.reggie.reggie.service.ShoppingCartService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/shoppingCart")
public class ShoppingCartController {

    @Autowired
    ShoppingCartService shoppingCartService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public R<List<ShoppingCart>> getShoppingCart(HttpSession session) {
        User user = (User) session.getAttribute("user");
        Long userId = user.getId();
        List<ShoppingCart> shoppingCarts = shoppingCartService.selectShoppingCartByUserId(userId);
        return R.success(shoppingCarts);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public R<ShoppingCart> addShoppingCart(@RequestBody ShoppingCart shoppingCart) {
        ShoppingCart r = shoppingCartService.addShoppingCart(shoppingCart);
        return R.success(r);
    }

    @RequestMapping(value = "/clean", method = RequestMethod.DELETE)
    public R<String> cleanShoppingCart() {
        shoppingCartService.cleanShoppingCart();
        return R.success();
    }

    @RequestMapping(value = "/sub", method = RequestMethod.POST)
    public R<ShoppingCart> subShoppingCart(@RequestBody ShoppingCart shoppingCart) {
        ShoppingCart r = shoppingCartService.subShoppingCart(shoppingCart);
        return R.success(r);
    }

}
